import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

// 球队类，存球队各种数据
class Team {
    private String name;    // 球队名字
    private int pg;         // 踢了几场比赛
    private int wg;         // 赢了几场
    private int dg;         // 平了几场
    private int lg;         // 输了几场
    private int gs;         // 进了几个球
    private int gc;         // 被进几个球
    private int gd;         // 净胜球（进球减失球）
    private int pts;        // 球队积分

    // 初始化球队，名字给定，其他数据设为 0
    public Team(String name) {
        this.name = name;
        this.pg = 0;
        this.wg = 0;
        this.dg = 0;
        this.lg = 0;
        this.gs = 0;
        this.gc = 0;
        this.gd = 0;
        this.pts = 0;
    }

    // 获取球队名字的方法
    public String getName() {
        return name;
    }

    // 获取比赛场次的方法
    public int getPlayedGames() {
        return pg;
    }

    // 设置比赛场次的方法
    public void setPlayedGames(int playedGames) {
        this.pg = playedGames;
    }

    // 获取胜场数的方法
    public int getWonGames() {
        return wg;
    }

    // 设置胜场数的方法
    public void setWonGames(int wonGames) {
        this.wg = wonGames;
    }

    // 获取平场数的方法
    public int getDrawGames() {
        return dg;
    }

    // 设置平场数的方法
    public void setDrawGames(int drawGames) {
        this.dg = drawGames;
    }

    // 获取负场数的方法
    public int getLostGames() {
        return lg;
    }

    // 设置负场数的方法
    public void setLostGames(int lostGames) {
        this.lg = lostGames;
    }

    // 获取进球数的方法
    public int getGoalsScored() {
        return gs;
    }

    // 设置进球数的方法
    public void setGoalsScored(int goalsScored) {
        this.gs = goalsScored;
    }

    // 获取失球数的方法
    public int getGoalsConceded() {
        return gc;
    }

    // 设置失球数的方法
    public void setGoalsConceded(int goalsConceded) {
        this.gc = goalsConceded;
    }

    // 获取球队积分的方法
    public int getPoints() {
        return pts;
    }

    // 设置球队积分的方法
    public void setPoints(int points) {
        this.pts = points;
    }

    // 获取净胜球数的方法
    public int getGoalDifference() {
        return gd;
    }

    // 设置净胜球数的方法
    public void setGoalDifference(int goalDifference) {
        this.gd = goalDifference;
    }

    // 根据比赛进球更新球队数据
    // 看主客队进球，更新比赛场次、进球、失球、净胜球、积分等数据
    public void updateStats(int homeGoals, int awayGoals) {
        pg++;          // 比赛一场，场次加 1
        gs += homeGoals;    // 加上主场进球数
        gc += awayGoals;    // 加上客场进球数（主场失球数）
        gd = gs - gc;        // 算净胜球

        if (homeGoals > awayGoals) {  // 主场赢了
            wg++;
            pts += 3;    // 赢了得 3 分
        } else if (homeGoals < awayGoals) {  // 主场输了
            lg++;
        } else {  // 平局
            dg++;
            pts += 1;    // 平了得 1 分
        }
    }
}

// 足球联赛积分管理主类
public class football {
    public static void main(String[] args) throws IOException {
        File file = new File("足球联赛积分表.txt");  // 积分表文件
        List<Team> teams = new ArrayList<>();  // 存球队对象的列表
        Scanner scanner = new Scanner(System.in);  // 用于读取控制台输入

        // 处理文件，没有就创建，有就读取数据并展示
        if (!file.exists()) {
            file.createNewFile();  // 创建新文件
            System.out.println("没找到积分表，正在创建...");
        } else {
            try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                String line;
                while ((line = br.readLine())!= null) {
                    String[] parts = line.split(" ");

                    // 从文件数据创建球队对象并设置属性
                    String teamName = parts[0];
                    int playedGames = Integer.parseInt(parts[1]);
                    int wonGames = Integer.parseInt(parts[2]);
                    int drawGames = Integer.parseInt(parts[3]);
                    int lostGames = Integer.parseInt(parts[4]);
                    int goalsScored = Integer.parseInt(parts[5]);
                    int goalsConceded = Integer.parseInt(parts[6]);
                    int goalDifference = Integer.parseInt(parts[7]);
                    int points = Integer.parseInt(parts[8]);

                    Team team = new Team(teamName);
                    team.setPlayedGames(playedGames);
                    team.setWonGames(wonGames);
                    team.setDrawGames(drawGames);
                    team.setLostGames(lostGames);
                    team.setGoalsScored(goalsScored);
                    team.setGoalsConceded(goalsConceded);
                    team.setGoalDifference(goalDifference);
                    team.setPoints(points);

                    teams.add(team);
                }
            }
            // 展示读取到的积分表数据
            System.out.println("原有积分表：");
            System.out.println("队名\t场次\t胜场\t平场\t负场\t进球\t失球\t净胜球\t积分");
            for (Team team : teams) {
                System.out.println(team.getName() + "\t" + team.getPlayedGames() + "\t" + team.getWonGames() + "\t" + team.getDrawGames() + "\t" + team.getLostGames() + "\t" + team.getGoalsScored() + "\t" + team.getGoalsConceded() + "\t" + team.getGoalDifference() + "\t" + team.getPoints());
            }
        }

        while (true) {
            System.out.println();
            System.out.println("输入比赛结果 (主队 客队 主队得分 客队得分):");
            String input = scanner.nextLine();
            if ("exit".equals(input)) {
                break;
            }
            String[] matchParts = input.split(" ");
            if (matchParts.length!= 4) {
                System.out.println("输入错了，重新输");
                continue;
            }
            String homeTeamName = matchParts[0];
            String awayTeamName = matchParts[1];
            int homeGoals;
            int awayGoals;
            try {
                homeGoals = Integer.parseInt(matchParts[2]);
                awayGoals = Integer.parseInt(matchParts[3]);
            } catch (NumberFormatException e) {
                System.out.println("输入错了，重新输");
                continue;
            }

            Team homeTeam = null;
            Team awayTeam = null;

            // 找或创建主队对象
            for (Team team : teams) {
                if (team.getName().equals(homeTeamName)) {
                    homeTeam = team;
                    break;
                }
            }
            if (homeTeam == null) {
                homeTeam = new Team(homeTeamName);
                teams.add(homeTeam);
            }

            // 找或创建客队对象
            for (Team team : teams) {
                if (team.getName().equals(awayTeamName)) {
                    awayTeam = team;
                    break;
                }
            }
            if (awayTeam == null) {
                awayTeam = new Team(awayTeamName);
                teams.add(awayTeam);
            }

            homeTeam.updateStats(homeGoals, awayGoals);
            awayTeam.updateStats(awayGoals, homeGoals);

            // 按积分、净胜球、进球数排序球队列表并展示积分表
            Collections.sort(teams, new Comparator<Team>() {
                @Override
                public int compare(Team t1, Team t2) {
                    if (t1.getPoints()!= t2.getPoints()) {
                        return Integer.compare(t2.getPoints(), t1.getPoints());
                    } else if (t1.getGoalDifference()!= t2.getGoalDifference()) {
                        return Integer.compare(t2.getGoalDifference(), t1.getGoalDifference());
                    } else {
                        return Integer.compare(t2.getGoalsScored(), t1.getGoalsScored());
                    }
                }
            });

            System.out.println();
            System.out.println("积分表：");
            System.out.println("队名\t场次\t胜场\t平场\t负场\t进球\t失球\t净胜球\t积分");
            for (Team team : teams) {
                System.out.println(team.getName() + "\t" + team.getPlayedGames() + "\t" + team.getWonGames() + "\t" + team.getDrawGames() + "\t" + team.getLostGames() + "\t" + team.getGoalsScored() + "\t" + team.getGoalsConceded() + "\t" + team.getGoalDifference() + "\t" + team.getPoints());
            }

            // 把更新后的球队数据写入文件
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
                for (Team team : teams) {
                    bw.write(team.getName() + " " + team.getPlayedGames() + " " + team.getWonGames() + " " + team.getDrawGames() + " " + team.getLostGames() + " " + team.getGoalsScored() + " " + team.getGoalsConceded() + " " + team.getGoalDifference() + " " + team.getPoints());
                    bw.newLine();
                }
            }
        }
    }
}